package com.txc.common.utils.supply;

import com.txc.common.global.result.Result;
import com.txc.common.utils.format.ObjectFormat;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @ClassName: ServerHttpResponseUtil
 * @Author: TXC
 * @Date: 2022-08-10 14:16
 **/
public class ServerHttpResponseUtil {
    public static Mono<Void> out(ServerHttpResponse response, Result<?> result)
    {
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        DataBuffer wrap = response.bufferFactory().wrap(Objects.requireNonNull(ObjectFormat.objectToByte(result)));
        return response.writeWith(Mono.fromSupplier(() -> wrap));
    }
}
